#include "PlayScene.h"

using namespace cocos2d;


void PlayScene::runThisScene()
{
	CCLayer * pLayer = new PlayLayer();
	pLayer->autorelease();
	
	addChild(pLayer);
	CCDirector::sharedDirector()->replaceScene(this);
}

PlayScene::PlayScene()
{
	PlayLayer * pLayer = PlayLayer::create();
	
	addChild(pLayer);
}

bool PlayLayer::init()
{
	if ( !CCLayer::init() )
    {
        return false;
    }
	
	// ask director the window size
    _size = CCDirector::sharedDirector()->getWinSize();

	CCSprite* pPlayBack = CCSprite::create("game_bg.png");
	pPlayBack->setPosition(ccp(_size.width/2, _size.height/2));
    
    this->addChild(pPlayBack, 0);


	_pMenu = CCMenu::create();

	CCMenuItemImage *pMenuItem = CCMenuItemImage::create(
                                        "Icon.png",
                                        NULL,
                                        this,
										menu_selector(PlayLayer::moveLeft) );
	pMenuItem->setPosition( ccp(_size.width * 0.1f, _size.height * 0.5f) );
	_pMenu->addChild(pMenuItem,NULL);

	
	pMenuItem = CCMenuItemImage::create(
                                        "Icon.png",
                                        NULL,
                                        this,
										menu_selector(PlayLayer::moveRight) );
	pMenuItem->setPosition( ccp(_size.width * 0.9f, _size.height * 0.5f) );
	_pMenu->addChild(pMenuItem,NULL);
	
    _pMenu->setPosition( CCPointZero );
    this->addChild(_pMenu, 1);


    _pPlayer = new Player();
    _pPlayer->setPosition(ccp(_size.width * 0.5f, 56.5f));
    this->addChild(_pPlayer, 1);
	
	_blockManager = new BlockManager();
//    _blockManager->setPosition(ccp(0.0f, 0.0f));
    this->addChild(_blockManager, 1);
    
    _ui = new PlayLayerUI();
    this->addChild(_ui, 2);
    
    this->schedule(schedule_selector(PlayLayer::update), 0.4f);
    
	return true;
}


void PlayLayer::moveLeft(CCObject* pSender)
{
	_pPlayer->move(PLAYER_MOVE_TYPE_LEFT);
}

void PlayLayer::moveRight(CCObject* pSender)
{
	_pPlayer->move(PLAYER_MOVE_TYPE_RIGHT);
}

void PlayLayer::update(float dt)
{
	_pPlayer->update(dt);
    CCRect playerRect = _pPlayer->boundingBox();
    
    _blockManager->update(dt);
    
    for(int i=0;i<_blockManager->_pBlocks->count();i++)
    {
        Block* block =(Block*)_blockManager->_pBlocks->objectAtIndex(i);
        CCRect blockRect = block->boundingBox();
        
        if(blockRect.intersectsRect(playerRect))
        {
            
//            if(block->_isAlive)
//            {
//                continue;
//            }
            
            _pPlayer->setBlock(block->_type);
            
            block->dead();
            
            _ui->setFCreditCount(_pPlayer->_fCreditCount);
            
            if(_pPlayer->_fCreditCount >= 3)
            {
                _pPlayer->breakdown();
                this->unscheduleAllSelectors();
                
                _ui->showResultWindow();
                
                _ui->_pScoreBoard->setScore(_pPlayer->_totalCredit);
            }
            
            break;
        }
    }

}
